﻿using MahApps.Metro.Controls.Dialogs;
using Microsoft.Win32;
using Public;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Xml.Linq;
using WpfMyText.M;
namespace WpfMyText.VM
{

    public class MainWindowVM : INotifyPropertyChanged
    {

        private AI aI;
        private RecentFilesManager recentFilesManager;
        public ICommand OpenFileCommand { get; }
        public ICommand OpenNewFileCommand { get; }


        private ObservableCollection<string> _menuItems;
        public ObservableCollection<string> MenuItems
        {
            get { return _menuItems; }
            set
            {
                _menuItems = value;
                OnPropertyChanged(nameof(MenuItems));
            }
        }
        public void AddMenuItem(string item)
        {

            recentFilesManager.AddRecentFile(item);
            MenuItems.Add(item);
            OnPropertyChanged(nameof(MenuItems));  // 手动触发属性更改通知
        }

        private string aIText;
        public string AIText
        {
            get
            {
                return aIText;
            }
            set
            {
                aIText = value;
                OnPropertyChanged();
            }
        }

        private string newWords = "初始word";
        public string NewWords
        {
            get
            {
                return newWords;
            }
            set
            {
                newWords = value;
                OnPropertyChanged();
            }
        }
        private string content = "";

        public string Content
        {
            get
            {
                return content;
            }
            set
            {
                content = value;
                OnPropertyChanged();
            }
        }
        public static string FilePath;


        public event PropertyChangedEventHandler? PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public MainWindowVM()
        {
            recentFilesManager = new RecentFilesManager();
            OpenFileCommand = new RelayCommand<string>(OpenFile);
            OpenNewFileCommand = new RelayCommand(OpenNewFile);
            MenuItems = new ObservableCollection<string>(recentFilesManager.GetRecentFiles());
        }

        private void OpenFile(string fileName)
        {
            // 模拟打开文件的操作
            FilePath = fileName;
            if (ReadALLText(fileName))
            {

            }


        }

        private void OpenNewFile()
        {

            StartCheck();

        }

        public bool ReadALLText(string filePath)
        {
            FilePath = filePath;

            try
            {
                Content = File.ReadAllText(filePath);
                return true;

            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("文件未找到！");
                return false;
            }
            catch (IOException e)
            {
                MessageBox.Show("读取文件时出错: " + e.Message);
                return false;
            }
        }
        public void AppendTextToFile(string text)
        {
            try
            {
                if (string.IsNullOrEmpty(FilePath))
                {
                    throw new Exception("没有可保存的文件！");
                }
                using (StreamWriter writer = File.AppendText(FilePath))
                {
                    writer.WriteAsync(text);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(("写入文件时出错: " + e.Message));
            }
        }
        public void WriteToFile()
        {
            try
            {
                if (File.Exists(FilePath))
                {
                    File.WriteAllText(FilePath, Content);
                }
            }
            catch (Exception e)
            {

            }


        }
        public bool StartCheck()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "文本文件|*.txt";
            openFileDialog.Title = "请选择一个文本文件";
            if (openFileDialog.ShowDialog() == true)
            {
                string filePath = openFileDialog.FileName;
                if (!File.Exists(filePath))
                {
                    return false;
                }
                else
                {
                    if (!MenuItems.Any(t => t == filePath))
                    {
                        this.AddMenuItem(filePath);
                    }

                    return ReadALLText(filePath);
                }
                // 在此处对选中的文件路径进行后续处理
            }
            else
            {
                return false;
            }


        }
        public async Task<bool> LetAIAppendText()
        {
            bool result = false;
            try
            {
                if (aI == null)
                {
                    aI = new AI("17600558273", "null");
                }
                AIText = await aI.AppendTextWithAIResponse(Content);
                if (!string.IsNullOrEmpty(AIText))
                {
                    result = true;
                }
            }
            catch
            {
                // Handle exceptions if necessary
            }
            return result;
        }
        public void AppendAIResponse()
        {
            if (!string.IsNullOrEmpty(content))
            {
                Content += AIText;
            }
        }
       
    }
   
}
